<?php 
namespace app\admin\controller;

use app\admin\model\NewsModel;
use app\admin\model\LabelModel;
use app\admin\model\ClassModel;
use app\admin\model\NewsLabelModel;
use app\admin\logic\NewsLogic;
/**
 * 文章管理
 */
class News extends Base
{
	
	public function index()
	{
		$news = new NewsModel();
		$logic = new NewsLogic();

		$type     = input("param.type/d");
		$pagenum  = input("param.page/d") ;
		$pagesize = input("param.limit/d");
		$status   = input("param.status/d");
		$title    = input("param.title/s");
		$class    = input("param.class/d");
		$label    = input("param.label/d");

		if ($type == 1) {
			$order    = "news_id desc";
			$pagenum  = $pagenum < 1 ? 1: $pagenum;
			$pagesize = $pagesize < 1 ? 10: $pagesize;
			$start    = ($pagenum - 1) * $pagesize;
			$size     = $pagesize;

			// 获取数据
			$result   = $logic->get_news_list($title, $class, $label, $status, $order, $start, $size);

            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $result['sum'];
            $data['data']   = $result['list'];

            echo json_encode($data);
            exit;
		}

		// 获取文章分类
		$category = $logic->get_news_class();
		// 获取所有标签
		$labels   = $logic->get_news_label();
		$data = [
			'classes' => $category,
			'labels'  => $labels,
			'status'  => $status,
		];

		return $this->fetch('index', $data);
	}
	
	// 编辑文章
	public function edit()
	{
		$news      = new NewsModel();
		$label     = new LabelModel();
		$newslabel = new NewsLabelModel();
		$logic     = new NewsLogic();
		// 提交数据处理
		$flag      = input("param.flag/s");
		$news_id   = input("param.news_id/d");


		if (request()->isAjax()) {

			// 获取数据
			$data = input("param.");
			// 编辑数据
			$res  = $logic->add_news($data, $flag);
			echo json_encode($res);
			exit;

		}

		// 获取文章分类
		$category = $logic->get_news_class();

		$data = [];
		$data['category'] = $category;
		if ($flag == "add") {
			
			return $this->fetch('add', $data);

		}elseif($flag == "edit"){
			// 获取该条数据详情
			$result = $news::get($news_id);
			$data['data'] = $result;

			// 获取标签
			$where  = ['news_id' => $news_id];
			$labels = $newslabel->get_list($where);
			if (!empty($labels)) {
				$label_arr = [];
				foreach ($labels as $k => $v) {
					$label_arr[] = $v['label_id'];
				}

				$where = "label_id IN (".implode(",", $label_arr).")";
				$res   = $label->get_list($where);

				// 传参
				$data['labelstr'] = implode(",", $label_arr);
				$data['labels'] = $res;
			}
            $data['content'] = html_entity_decode($data['content'] );
			//dump($data);exit;
			return $this->fetch('edit', $data);
		}
	}

	// 删除文章
	public function delnews()
	{
		$news   = new NewsModel();
		$id     = input("param.news_id/d");
		$ids    = input("param.ids");
		$status = input("param.status/d");

		if (!empty($id)) {
			if ($status == 3) {
				$res = $news->del_data(['news_id' => $id]);
				if (isset($res['msg'])) {
					echo json_encode($res);exit;
				}
			}else{
				$where = [
					'news_id' => $id,
					'status' => 3,
				];
				$res = $news->save_data($where);
				if (isset($res['msg'])) {
					echo json_encode($res);exit;
				}				
			}

		}

		if (!empty($ids)) {
			$ids  = json_decode($ids);
			if ($status == 3) {
				$where = "news_id IN (".implode(",", $ids).")";
				$res = $news->del_data($where);
				if (isset($res['msg'])) {
					echo json_encode($res);exit;
				}
			}else{
				$cond = [];
				foreach ($ids as $k => $v) {
					$arr = [
						'news_id' => $v,
						'status' => 3,
					];

					$cond[] = $arr;
				}

				$res = $news->edit_data_bantch($cond);
				if (isset($res['msg'])) {
					echo json_encode($res);exit;
				}				
			}
		}

		if ($status == 3) {
			echo json_encode(msg_success("删除成功！"));exit;
		}else{
			echo json_encode(msg_success("移至回收站！"));exit;
		}
	}




	// 分类管理
	public function classes()
	{	
		$class = new ClassModel();
		// param
		$type     = input("param.type/d");
		$pagenum  = input("param.page/d") ;
		$pagesize = input("param.limit/d");

		if ($type == 1) {
			$pagenum  = $pagenum < 1 ? 1: $pagenum;
			$pagesize = $pagesize < 1 ? 10: $pagesize;
			$start    = ($pagenum - 1) * $pagesize;
			$size     = $pagesize;
			$where    = [];
			$order    = "class_id desc";
			$fields   = "*";
			// 查询数据
			$sum     = $class->get_count($where);
			$res     = $class->get_list_page($where, $order, $fields, $start, $size);

            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $sum;
            $data['data']   = $res;

            echo json_encode($data);
            exit;
		}

		return $this->fetch('class');
	}

	// 分类编辑
	public function edit_class()
	{
		// 提交数据处理
		$flag     = input("param.flag/s");
		$class_id = input("param.class_id/d");
		$class    = new ClassModel();

		if (request()->isAjax()) {

			// 获取数据
			$data = input("param.");
			$data['status'] = empty($data['status'])? -1: $data['status'];

			if ($flag == "add") {

				$res  = $class->insert_data($data);// 添加数据
				if (isset($res['msg'])){
					echo json_encode($res);	
					exit;
				}
					
			}elseif($flag == "edit"){
				$res  = $class->save_data($data);// 更新数据
				if (isset($res['msg'])){
					echo json_encode($res);	
					exit;
				}
			}

			echo json_encode(msg_success("编辑成功"));
			exit;
		}

		$data = [];
		if ($flag == "add") {

			return $this->fetch('class_add');

		}elseif($flag == "edit"){
			// 获取该条数据详情
			$result = $class::get($class_id);
			$data['data'] = $result;

			return $this->fetch('class_edit', $data);
		}
	}

	// 删除分类
	public function delclass()
	{
		$class = new ClassModel();
		$id   = input("param.class_id/d");
		$ids  = input("param.ids");

		if (!empty($id)) {
			$where = ['class_id' => $id];
		}

		if (!empty($ids)) {
			$ids   = json_decode($ids);
			$where = "class_id IN (".implode(",", $ids).")";
		}

		// 删除数据
		$res = $class->del_data($where);
		if (isset($res['status'])) {
			echo json_encode($res);
		}else{
			echo json_encode(msg_success("删除成功"));
		}
		exit;
	}

	// 标签列表
	public function label_list()
	{
		$label = new LabelModel();
		$val = input("param.label/s");
		if (request()->isAjax()) {

			$where = ['status' => 1];
			$list  = $label->get_list($where);
			if (!empty($val)) {
				$arr = explode(",", $val);
				foreach ($list as $k => $v) {
					if (in_array($v['label_id'], $arr)) {
						$list[$k]['select'] = 1;
					}
				}	
			}

			echo json_encode($list);exit;
		}
	}

	// 标签选择
	public function label_select()
	{
		$label = new LabelModel();
		$val = input("param.label/a");

		if (request()->isAjax()) {
			$str   = implode(",", $val);
			$where = "label_id in (".$str.")";
			$list  = $label->get_list($where);

			$data  = [];
			$data['str']  = $str;
			$data['list'] = $list;

			echo json_encode($data);exit;
		}
	}

	// 标签管理
	public function label()
	{	
		$label = new LabelModel();
		// param
		$type     = input("param.type/d");
		$pagenum  = input("param.page/d") ;
		$pagesize = input("param.limit/d");

		if ($type == 1) {
			$pagenum  = $pagenum < 1 ? 1: $pagenum;
			$pagesize = $pagesize < 1 ? 10: $pagesize;
			$start    = ($pagenum - 1) * $pagesize;
			$size     = $pagesize;
			$where    = [];
			$order    = "label_id desc";
			$fields   = "*";
			// 查询数据
			$sum     = $label->get_count($where);
			$res     = $label->get_list_page($where, $order, $fields, $start, $size);

            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $sum;
            $data['data']   = $res;

            echo json_encode($data);
            exit;
		}

		return $this->fetch('label');
	}

	// 标签编辑
	public function edit_label()
	{
		// 提交数据处理
		$flag     = input("param.flag/s");
		$label_id = input("param.label_id/d");
		$label    = new LabelModel();

		if (request()->isAjax()) {

			// 获取数据
			$data = input("param.");
			$data['status'] = empty($data['status'])? -1: $data['status'];

			if ($flag == "add") {

				$res  = $label->insert_data($data);// 添加数据
				if (isset($res['msg'])){
					echo json_encode($res);	
					exit;
				}
					
			}elseif($flag == "edit"){
				$res  = $label->save_data($data);// 更新数据
				if (isset($res['msg'])){
					echo json_encode($res);	
					exit;
				}
			}

			echo json_encode(msg_success("编辑成功"));
			exit;
		}

		$data = [];
		if ($flag == "add") {

			return $this->fetch('label_add');

		}elseif($flag == "edit"){
			// 获取该条数据详情
			$result = $label::get($label_id);
			$data['data'] = $result;

			return $this->fetch('label_edit', $data);
		}
	}

	// 删除标签
	public function delabel()
	{
		$label = new LabelModel();
		$id   = input("param.label_id/d");
		$ids  = input("param.ids");

		if (!empty($id)) {
			$where = ['label_id' => $id];
		}

		if (!empty($ids)) {
			$ids   = json_decode($ids);
			$where = "label_id IN (".implode(",", $ids).")";
		}

		// 删除数据
		$res = $label->del_data($where);
		if (isset($res['status'])) {
			echo json_encode($res);
		}else{
			echo json_encode(msg_success("删除成功"));
		}
		exit;
	}

	
}


 ?>